package com.gec.controller;


import com.gec.entity.MenuTree;
import com.gec.entity.SysPermission;
import com.gec.entity.SysRole;
import com.gec.entity.SysRolePermission;
import com.gec.service.SysRoleService;
import com.gec.service.SysService;
import com.gec.service.workflow.WorkFlowService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author GOF
 * @since 2021-11-17
 */
@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private WorkFlowService wfs;

    @Autowired
    private SysService sysService;

    @RequestMapping("/workflow_list")
    public String workFlowList(Model model) {
        //查询全部流程定义
        List<ProcessDefinition> pdList = wfs.findProcessDefinitionList();
        model.addAttribute("pdList", pdList);

        //查询全部已部署流程
        List<Deployment> depList = wfs.findDeploymentList();
        model.addAttribute("depList", depList);

        return "workflow_list";
    }


    @RequestMapping("/add_process")
    public String addProcess() {
        return "add_process";
    }

    @PostMapping("/add_process")
    public String addProcess(String processName, MultipartFile processFile) throws IOException {

        wfs.saveNewDeploye(processFile.getInputStream(), processName);

        return "redirect:/employee/workflow_list";
    }

    @RequestMapping("/delDeployment/{depId}")
    public String delDeployment(@PathVariable("depId")String depId){
        wfs.deleteProcessDefinitionByDeploymentId(depId);
        return "redirect:/employee/workflow_list";
    }

    @RequestMapping("/viewImage/{depId}/{imageName}")
    public String viewImage(@PathVariable("depId")String depId, @PathVariable("imageName")String imageName, HttpServletResponse response) throws IOException {
        //获取图片输入流
        InputStream imageInputStream = wfs.findImageInputStream(depId,imageName);

        //流转换
        IOUtils.copy(imageInputStream,response.getOutputStream());

        //关闭资源
        response.getOutputStream().close();
        imageInputStream.close();
        return null;
    }

    @RequestMapping("/findRoles")
    public String findRoles(Model model){

        List<SysRole> roles = sysService.findAllRoles();

        model.addAttribute("roles",roles);

        List<MenuTree> menu = sysService.getMenuTree();

        model.addAttribute("menus",menu);
        return "permissionlist";
    }

    @RequestMapping(value = "/loadMyPermissions",method = RequestMethod.POST)
    public @ResponseBody List<SysPermission> loadMyPermissions(String roleId){
        System.out.println("查询本条角色的权限......");
        return sysService.findpermissionsByRoleId(roleId);
    }

    @RequestMapping("/updateRoleAndPermission")
    public String updateRoleAndPermission(SysRolePermission rolePermission){
        System.out.println(rolePermission);
        return null;
    }
}

